﻿using QLKetNoi;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace QLDVu.GUI
{
    public partial class ThemPhieuThuForm : Form
    {
        public ThemPhieuThuForm()
        {
            InitializeComponent();
        }
        public static string NextID(string lastID, string prefixID)
        {
            int nextID = int.Parse(lastID.Remove(0, prefixID.Length)) + 1;
            int lengthNumerID = lastID.Length - prefixID.Length;
            string zeroNumber = "";
            for (int i = 1; i <= lengthNumerID; i++)
            {
                if (nextID < Math.Pow(10, i))
                {
                    for (int j = 1; j <= lengthNumerID - i; i++)
                    {
                        zeroNumber += "0";
                    }
                    return prefixID + zeroNumber + nextID.ToString();
                }
            }
            return prefixID + nextID;
        }
        private void btnThem_Click(object sender, EventArgs e)
        {
            bool kt = false;
            if (txtNguoiNopTien.Text.Trim() == "")
            {
                MessageBox.Show("Bạn phải nhập người nộp tiền");
                kt = true;
                return;
            }
            if (txtSoTienNop.Text.Trim() == "")
            {
                MessageBox.Show("Bạn phải nhập số tiền nộp");
                kt = true;
                return;
            }
            if (txtDienThoai.Text.Trim() == "")
            {
                MessageBox.Show("Bạn phải nhập số điện thoại");
                kt = true;
                return;
            }
            if (txtDiaChi.Text.Trim() == "")
            {
                MessageBox.Show("Bạn phải nhập địa chỉ");
                kt = true;
                return;
            }
            if (Convert.ToInt32(txtConLai.Text) < 0)
            {
                MessageBox.Show("Số tiền còn lại không được nhỏ hơn 0");
                kt = true;
                return;
            }
            if (kt == false)
            {
                string maphieuthu = "";
                if (BO.PhieuThuTienBO.MaPhieuThuCuoiCung() != null)
                    maphieuthu = NextID(BO.PhieuThuTienBO.MaPhieuThuCuoiCung().MaPhieuThuTien, "PT");
                else maphieuthu = "PT0001";
                BO.PhieuThuTienBO.ThemPhieuThuTien(maphieuthu, cbNhanVien.SelectedValue.ToString(), txtNguoiNopTien.Text.Trim(), cbPhieuSuaChua.SelectedValue.ToString(), dpNgayNop.Value.ToShortDateString(), txtSoTienNop.Text.Trim(), txtConLai.Text.Trim(),txtDienThoai.Text.Trim(),txtDiaChi.Text.Trim());
                BO.PhieuThuTienBO.CapNhatPhieuSuaChua(cbPhieuSuaChua.SelectedValue.ToString(), txtConLai.Text.Trim());
                txtNguoiNopTien.Text = "";
                txtSoTienNop.Text = "0";
                txtDiaChi.Text = "";
                txtDienThoai.Text = "";
                SUACHUA ps = BO.PhieuThuTienBO.PhieuSuaChuaTheoMa(cbPhieuSuaChua.SelectedValue.ToString());
                BAOGIA bg = BO.PhieuThuTienBO.PhieuBaoGiaTheoMa(ps.MaBaoGia);
                txtTongTien.Text = Convert.ToInt32(bg.TongTien).ToString();
                txtConLai.Text = Convert.ToInt32(ps.ConLai).ToString();
                MessageBox.Show("Bạn đã thềm thành công");
            }
        }

        private void ThemPhieuThuForm_Load(object sender, EventArgs e)
        {
            try
            {
                var ds = from psc in BO.PhieuThuTienBO.DanhSachPhieuSuaChua()
                         select new { psc.MaSuaChua, psc.BAOGIA.KHACHHANG.TenKhachHang, psc.BAOGIA.XE.BienSoXe };
                cbPhieuSuaChua.DataSource = ds;
                cbPhieuSuaChua.DisplayMember = "MaSuaChua";
                cbPhieuSuaChua.ValueMember = "MaSuaChua";
                cbPhieuSuaChua.DropDownColumns = "MaSuaChua,TenKhachHang,BienSoXe";
                cbPhieuSuaChua.DropDownColumnsHeaders = "Phiếu Sữa Chữa\r\nTên Khách Hàng\r\nBiển Số";
                cbPhieuSuaChua.DropDownWidth = 300;

                cbNhanVien.DataSource = BO.DanhSachNhanVienBO.DanhSachNhanVien();
                cbNhanVien.DisplayMember = "TenNguoiDung";
                cbNhanVien.ValueMember = "MaNguoiDung";
                cbNhanVien.DropDownColumns = "MaNguoiDung,TenNguoiDung";
                cbNhanVien.DropDownColumnsHeaders = "Mã Nhân Viên\r\nTên Nhân Viên\r\n";
                cbNhanVien.DropDownWidth = 200;

                dpNgayNop.CustomFormat = "dd/MM/yyyy";
                dpNgayNop.Format = DateTimePickerFormat.Custom;

                txtSoTienNop.Text = "0";
                SUACHUA ps = BO.PhieuThuTienBO.PhieuSuaChuaTheoMa(cbPhieuSuaChua.SelectedValue.ToString());
                BAOGIA bg = BO.PhieuThuTienBO.PhieuBaoGiaTheoMa(ps.MaBaoGia);
                txtTongTien.Text = Convert.ToInt32(bg.TongTien).ToString();
                txtConLai.Text = Convert.ToInt32(ps.ConLai).ToString();
            }
            catch
            {
            }
           
        }

        private void cbPhieuSuaChua_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                if (cbPhieuSuaChua.SelectedValue.ToString().Contains('{') == false)
                {
                    SUACHUA ps = BO.PhieuThuTienBO.PhieuSuaChuaTheoMa(cbPhieuSuaChua.SelectedValue.ToString());
                    BAOGIA bg = BO.PhieuThuTienBO.PhieuBaoGiaTheoMa(ps.MaBaoGia);
                    txtTongTien.Text = Convert.ToInt32(bg.TongTien).ToString();
                    txtConLai.Text = Convert.ToInt32(ps.ConLai).ToString();
                }
            }
            catch
            {

            }
        }

        private void txtSoTienNop_TextChanged(object sender, EventArgs e)
        {
            try
            {
                if (txtSoTienNop.Text.Trim() != "")
                {

                    SUACHUA ps = BO.PhieuThuTienBO.PhieuSuaChuaTheoMa(cbPhieuSuaChua.SelectedValue.ToString());
                    int conlai = Convert.ToInt32(ps.ConLai);
                    txtConLai.Text = (conlai - Convert.ToInt32(txtSoTienNop.Text.Trim())).ToString();
                }
            }
            catch
            {
            }
            
        }

        private void txtSoTienNop_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;
            }
        }

        private void labelX8_Click(object sender, EventArgs e)
        {

        }

        private void panelEx2_Click(object sender, EventArgs e)
        {

        }

        private void label3_Click(object sender, EventArgs e)
        {

        }

        private void txtDiaChi_TextChanged(object sender, EventArgs e)
        {

        }

        private void btnIn_Click(object sender, EventArgs e)
        {
            InPhieuThuTienForm inphieuthutien = new InPhieuThuTienForm();
            inphieuthutien.Show();
        }

        private void cbNhanVien_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void txtTongTien_TextChanged(object sender, EventArgs e)
        {

        }

        private void txtConLai_TextChanged(object sender, EventArgs e)
        {

        }
    }
}
